import { StoreService, queryOption } from './store.service';
// export class Model {
//   public ModelID: string;
//   public Name: string;
//   public IsPublic: boolean;
//   public Grade: number;
//   public SharedUser: string;
//   public Query: string;
//   public CreateTime: Date;
//   public Owner: string;
//   public LastExecuteTime: Date;
//   public Type: string;
//   public PublicColumns: string;
//   public Description: string;
//   public result: string;
//   constructor() {
//     this.CreateTime = new Date();
//     this.Description = "";
//     this.Grade = 0;
//     this.IsPublic = false;
//     this.LastExecuteTime = new Date(1970, 1, 1);
//     this.ModelID = "";
//     this.Name = "Unnamed Model";
//     this.Owner = "";
//     this.PublicColumns = "";
//     this.Query = "";
//     this.SharedUser = "";
//     this.Type = "aggregation";
//     this.result = "";
//   }
//   public getPublicConditions() {
//     if (this.PublicColumns === undefined || this.PublicColumns === "")
//       return [];
//     let cl = this.PublicColumns.split(",");
//     let rl = [];
//     cl.forEach((v) => {
//       if (this.Type === "aggregation") {
//         let agg = JSON.parse(this.Query) as Aggregation;
//         agg.conditionpipe.forEach((cs) => {
//           cs.conditions.forEach((c) => {
//             if (c.Show === v) rl.push(c);
//           });
//         });
//       }
//       if (this.Type == "search") {
//         let query = JSON.parse(this.Query) as Query;
//         query.conditionpipe.forEach((cs) => {
//           cs.conditions.forEach((c) => {
//             if (c.Show === v) rl.push(c);
//           });
//         });
//       }
//     });
//     return rl;
//   }
// }
export class QueryPackage {
  public id: string;
  public name: string;
  public shared: boolean;
  public grade: number;
  public sharedUser: string;
  public query: string; //json string
  public createtime: Date;
  public owner: string;
  public lastExecuteTime: Date;
  public description: string;
  public type: string;
  public version: number;
  public running: boolean;
  public showResult: boolean;
  public result: any;
  public publicCondition: Condition[];
  constructor() {
    this.id = undefined;
    this.name = 'unnamed';
    this.shared = false;
    this.sharedUser = '';
    this.createtime = new Date();
    this.owner = '';
    this.description = '';
    this.grade = 0;
    this.lastExecuteTime = new Date(1970, 1, 1);
    this.query = '';
    this.running = false;
    this.result = undefined;
    this.showResult = false;
    this.publicCondition = [];
    this.version = 1.0;
  }
  // public isQuery(): boolean {
  //   if (this.type === "query") return true;
  //   return false;
  // }
  // public isAggregation(): boolean {
  //   if (this.type === "aggregation") return true;
  //   return false;
  // }
  // public getQueryObject(): any {
  //   if (this.query === undefined || this.query === "") {
  //     if (this.isQuery()) return JSON.parse(this.query) as Query;
  //     if (this.isAggregation()) return JSON.parse(this.query) as Aggregation;
  //   }
  // }
  // public setQueryObject(obj: any) {
  //   console.log(obj);
  //   console.log(obj instanceof Query);
  //   console.log(obj as Query);
  //   console.log(obj as Aggregation);
  //   console.log(typeof obj);

  //   if (obj instanceof Query) {
  //     this.type = "query";
  //     this.query = JSON.stringify(obj);
  //   }
  //   if (obj instanceof Aggregation) {
  //     this.type = "aggregation";
  //     this.query = JSON.stringify(obj);
  //   }
  // }
}
export class ShowCondition {
  public show: boolean;
  public conditionTarget: string;
  constructor() {
    this.show = true;
    this.conditionTarget = '';
  }
}
export class Condition {
  constructor() {
    this.Target = '';
    this.Relation = 'And';
    this.Operation = 'Eq';
    this.Value = '';
    this.Show = '';
    this.DicTable = '';
    this.DicColumns = '';
    this.Db = '';
    this.DataType = '';
    this.Options = [];
    this.AddtionalValue = '';
    this.Public = false;
    this.IsShow = true;
  }
  public Target: string;
  public Relation: string;
  public Operation: string;
  public Value: string;
  public Show: string;
  public DicTable: string;
  public DicColumns: string;
  public Db: string;
  public DataType: string;
  public Options: queryOption[];
  public AddtionalValue: string;
  public Public: boolean;
  public IsShow: boolean;
}
//basic query, include conditionpipeline that is a group of condition, each group will filter data sequence from top to bottom
export class QueryParameter {
  isMridMode: boolean;
  distinctMrid: false;
  sequence: boolean;
  strict: boolean;
  limit: {
    Diagnosis: 0;
    VAllergyDiagnosis: 0;
    VClinicDiagnosis: 0;
    VOperate: 0;
    VSurgery: 0;
    VTransfer: 0;
    VContact: 0;
    VICU: 0;
  };
  constructor() {
    this.isMridMode = false;
    this.distinctMrid = false;
    this.sequence = false;
    this.strict = false;
    this.limit = {
      Diagnosis: 0,
      VAllergyDiagnosis: 0,
      VClinicDiagnosis: 0,
      VOperate: 0,
      VSurgery: 0,
      VTransfer: 0,
      VContact: 0,
      VICU: 0,
    };
  }
}
export class Query {
  public name: string;
  //public description: string;
  //public shared: boolean;
  //public ownerId: string;
  //public createTime: Date;
  //public lastExecuteTime: Date;
  public conditionpipe: ConditionGroup[];
  public showColumn: string[];
  public parameter: QueryParameter;
  public version: number;
  // public isLastestVersion(): boolean {
  //   if (this.version === 2.0) return true;
  //   return false;
  // }
  constructor() {
    // super();
    this.name = 'UntitledQuery';
    // this.description = "";
    // this.shared = false;
    // this.ownerId = "";
    // this.createTime = new Date();
    this.conditionpipe = [];
    this.showColumn = [];
    this.parameter = new QueryParameter();
    //this.type = "query";
    this.version = 3.0;
  }
}

export class Result {
  public data: object[];
  public showList: string[];
  public count: number;
  public pages: number;
  public currentPage: number;
  public countPerPage: number;
  public spendTime: number; //ms
}

export class StatisticResult {
  public name: string;
  public results: any;
  constructor() {
    this.name = '';
    this.results = {};
  }
}
export class StatisticTerm {
  public statistictype: string; //计数，分组计数，联合分组计数，平均，最大，最小
  public target: string; //for text must use keyword
  public name: string;
  public parameter: string;
  public show: string;
  public datatype: string;
  constructor() {
    this.statistictype = '';
    this.target = '';
    this.name = '';
    this.parameter = '';
    this.show = '';
    this.datatype = '';
  }
}
export class CalculateTerm {
  public form: string; //for text must use keyword
  public name: string;
  public parameter: string;
  constructor() {
    this.name = '';
    this.parameter = '';
    this.form = '';
  }
}
export class Statistic {
  public terms: StatisticTerm[];
  public conditions: Condition[]; //叠加额外过滤,just intersect
  public caculateterms: CalculateTerm[];
  constructor() {
    this.conditions = [];
    this.terms = [];
    this.caculateterms = [];
  }
  //public missing: string; //0或者ignor
  // public script: string;
  //public value: number;
  //public statistics: Statistic[];
}

export class ConditionGroup {
  public name: string;
  public relation: string; //default is intercect, include intercect, except ,union
  public conditions: Condition[];
  constructor() {
    this.name = '';
    this.relation = 'Intercect';
    this.conditions = [];
  }
}

export class Aggregation {
  public name: string;
  //public description: string;
  //public shared: boolean;
  //public createTime: Date;
  public statistics: Statistic[];
  public conditionpipe: ConditionGroup[];
  public expression: string; //addtional expression to calculate specific value, variable must be surrounded by {{}}
  //public lastExecuteTime: Date;
  public parameter: QueryParameter;
  public version: number;
  //public type: string;
  constructor() {
    // super();
    this.name = 'UntitledAgg';
    //this.description = "";
    //this.shared = false;
    //this.createTime = new Date();
    this.statistics = [];
    this.conditionpipe = [];
    this.expression = '';
    this.parameter = new QueryParameter();
    this.version = 3.0;
    //this.type = "agg";
  }
}
export class Diagnosis {
  public ICDName: string;
  public IDCCode: string;
  public internalICDName: string;
  public internalICDCode: string;
}
export class Operation {
  public ICDName: string;
  public IDCCode: string;
  public internalICDName: string;
  public internalICDCode: string;
  public operator: string;
  public opserateTime: Date;
}
// export class Surge {
//   public ICDName: string;
//   public IDCCode: string;
//   public internalICDName: string;
//   public internalICDCode: string;
//   public operator: string;
//   public opserateTime: Date;
// }

// export class MedicalRecord {
//   public mrid: string;
//   public patientName: string;
//   public gender: string;
//   public birthday: Date;
//   public age: number;
//   public attendingDoctor: string;
//   public coder: string;
//   public chiefDoctor: string;
//   public totalCost: number;
//   public addmittedTime: Date;
//   public dischargeTime: Date;
//   public addmittedDepartment: string;
//   public dischargeDepartment: string;

//   public clinicalDiagnostics: string;
//   public dischargeDiagnostics: Diagnosis[];
//   public operations: Operation[];
//   public surged: Surge[];
// }
// export class MedicalRecordShow {
//   public constructor() {}
//   public loadData(data) {
//     this.mrid = data['mrid'];
//     this.patientName = data['patientName'];
//     this.gender = data['gender'];
//     this.birthday = data['birthday'];
//     this.age = data['age'];
//     this.attendingDoctor = data['Doctor']['主治医生'];
//     this.coder = data['Doctor']['编码员'];
//     this.chiefDoctor = data['Doctor']['主任医生'];
//     this.totalCost = data['VFee']['总费用'];
//     this.coderTime = data['Doctor']['编码员']['CreateTime'];
//     for (
//       let index = 0;
//       index < data.Diagnosis.Sort((v) => v['DiagnosisOrder']).length;
//       index++
//     ) {
//       const element = data.Diagnosis[index];
//       this['ICDName' + (index + 1).toString] = element['ICDName'];
//       this['InernalICDName' + (index + 1).toString] = element['InernalICDName'];
//       this['ICDCode' + (index + 1).toString] = element['ICDCode'];
//       this['InernalICDCode' + (index + 1).toString] = element['InernalICDCode'];
//     }
//     for (
//       let index = 0;
//       index < data.VOperate.Sort((v) => v['OperateOrder']).length;
//       index++
//     ) {
//       const element = data.VOperate[index];
//       this['OperateICDCode' + (index + 1).toString] = element['OperateICDCode'];
//       this['OperateICDName' + (index + 1).toString] = element['OperateICDName'];
//       this['InernalICDCode' + (index + 1).toString] = element['InernalICDCode'];
//       this['InernalICDName' + (index + 1).toString] = element['InernalICDName'];
//     }
//     for (
//       let index = 0;
//       index < data.VSurgery.Sort((v) => v['OperateOrder']).length;
//       index++
//     ) {
//       const element = data.VSurgery[index];
//       this['OperateICDCode' + (index + 1).toString] = element['OperateICDCode'];
//       this['OperateICDName' + (index + 1).toString] = element['OperateICDName'];
//       this['InernalICDCode' + (index + 1).toString] = element['InernalICDCode'];
//       this['InernalICDName' + (index + 1).toString] = element['InernalICDName'];
//     }
//   }
//   public mrid: string;
//   public patientName: string;
//   public gender: string;
//   public birthday: Date;
//   public age: number;
//   public attendingDoctor: string;
//   public coder: string;
//   public chiefDoctor: string;
//   public totalCost: number;
//   public addmittedTime: Date;
//   public dischargeTime: Date;
//   public addmittedDepartment: string;
//   public dischargeDepartment: string;
//   public coderTime: Date;
//   public clinicalDiagnostics: string;
// }

export class DataPoint {
  public label: string;
  public data: number;
  constructor(_lable: string, _data: number) {
    this.label = _lable;
    this.data = _data;
  }
}
